<?php

// 应用公共文件
function p($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

//删除任何文件夹的函数
//$path是需要删除文件夹的路径
function my_rmdir($path)
{
    //先读取文件夹里面的文件
    $arr = scandir($path);
    //p($arr);
    foreach ($arr as $v) {
        if ($v == '.' || $v == '..') {
            continue;
        }
        //拼接完整路径
        $file = $path . '/' . $v;
        //echo $file.'<br>';
        if (is_file($file)) { //如果是文件
            unlink($file);
        } else {//如果是文件夹
            my_rmdir($file);
        }
    }
    rmdir($path);//删除空文件夹
}

//弹窗函数
//$msg提醒的文本内容
//$url跳转的页面地址
function show_msg($msg, $url)
{
    echo '<script>alert("' . $msg . '");location.href="' . $url . '";</script>';
    exit();
}

/**
 *
 * 字符截取
 * @param string $string 需要截取的字符串
 * @param int $start 截取的开始位置
 * @param int $length 截取的长度
 * @param string $charset 编码
 * @param string $dot 如果截取的长度小于总长度就加这个字符
 */
function str_cut(&$string, $start, $length, $charset = "utf-8", $dot = '...')
{
    if (function_exists('mb_substr')) {
        if (mb_strlen($string, $charset) > $length) {
            return mb_substr($string, $start, $length, $charset) . $dot;
        }
        return mb_substr($string, $start, $length, $charset);
    } else if (function_exists('iconv_substr')) {
        if (iconv_strlen($string, $charset) > $length) {
            return iconv_substr($string, $start, $length, $charset) . $dot;
        }
        return iconv_substr($string, $start, $length, $charset);
    }
    $charset = strtolower($charset);
    switch ($charset) {
        case "utf-8" :
            preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $ar);
            if (func_num_args() >= 3) {
                if (count($ar[0]) > $length) {
                    return join("", array_slice($ar[0], $start, $length)) . $dot;
                }
                return join("", array_slice($ar[0], $start, $length));
            } else {
                return join("", array_slice($ar[0], $start));
            }
            break;
        default:
            $start = $start * 2;
            $length = $length * 2;
            $strlen = strlen($string);
            for ($i = 0; $i < $strlen; $i++) {
                if ($i >= $start && $i < ($start + $length)) {
                    if (ord(substr($string, $i, 1)) > 129) $tmpstr .= substr($string, $i, 2);
                    else $tmpstr .= substr($string, $i, 1);
                }
                if (ord(substr($string, $i, 1)) > 129) $i++;
            }
            if (strlen($tmpstr) < $strlen) $tmpstr .= $dot;

            return $tmpstr;
    }
}

//二维数组排序
function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
{
    if (is_array($arrays)) {
        foreach ($arrays as $array) {
            if (is_array($array)) {
                $key_arrays[] = $array[$sort_key];
            } else {
                return false;
            }
        }
    } else {
        return false;
    }
    array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
    return $arrays;
}


/*
    * msubstr 函数
    * 作用：截取字符输出
    * 参数1 需要截取的字符串
    * 参数2 从第几个开始
    * 参数3 截取的长度
    * 参数4 字符串编码 utf-8下 3个字符 = 中文  gbk 2个字符 = 1中文
    * 参数5 是否显示...
*/
function msubstr($str, $start = 0, $length, $charset = "utf-8", $suffix = true)
{
    if (function_exists("mb_substr")) {
        if ($suffix) {
            if ($str == mb_substr($str, $start, $length, $charset)) {
                return mb_substr($str, $start, $length, $charset);
            } else {
                return mb_substr($str, $start, $length, $charset) . "...";
            }
        } else {
            return mb_substr($str, $start, $length, $charset);
        }
    } elseif (function_exists('iconv_substr')) {
        if ($suffix) {
            if ($str == iconv_substr($str, $start, $length, $charset)) {
                return iconv_substr($str, $start, $length, $charset);
            } else {
                return iconv_substr($str, $start, $length, $charset) . "...";
            }
        } else {
            return iconv_substr($str, $start, $length, $charset);
        }
    }
    $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    if ($suffix) return $slice . "…";
    return $slice;
}
